package com.zhongc.advance.api.impl;

import com.zhongc.advance.api.SecurityQuestionService;
import com.zhongc.advance.common.CommonCode;
import com.zhongc.advance.common.result.Result;
import com.zhongc.advance.common.result.Results;
import com.zhongc.advance.common.result.Status;
import com.zhongc.advance.common.util.StringUtil;
import com.zhongc.advance.service.AdvanceSecurityQuestionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

/**
 * @Description
 * @Version 1.0
 * @Author zhongc
 * @Email zhong_ch@foxmail.com
 * @History <li>Author: zhongc</li>
 * <li>Date: 2015-07-27</li>
 * <li>Version: 1.0</li>
 * <li>Content: create</li>
 */
@Service("securityQuestionService")
public class SecurityQuestionServiceImpl implements SecurityQuestionService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private AdvanceSecurityQuestionService advanceSecurityQuestionService;

    @Override
    public Results selectQuestion() {
        logger.info("查询问题入参");
        Results results = new Results();
        try {
            results.setResult(advanceSecurityQuestionService.selectQuestions());
            results.setCode(CommonCode.SUCCESS);
            results.setStatus(Status.SUCCESS);
            results.setDescription("查询问题成功！");
        } catch (DataAccessException e) {
            results.setCode(CommonCode.FAIL);
            results.setStatus(Status.FAIL);
            results.setDescription("查询失败：数据库出现问题");
            logger.error("查询问题失败 原因 -->", e.getMessage());
        } catch (Exception e) {
            results.setCode(CommonCode.FAIL);
            results.setStatus(Status.FAIL);
            results.setDescription("查询失败：检查日志");
            logger.error("查询问题失败 原因 -->", e.getMessage());
        }
        return results;
    }

    @Override
    public Result addQuestion(String question) {
        logger.info("添加问题入参 question->{}", question);
        Result result = new Result();
        if (!StringUtil.isNotNull(question)) {
            result.setCode(CommonCode.FAIL);
            result.setStatus(Status.FAIL);
            result.setDescription("添加问题失败、问题不能为空");
        } else {
            try {
                advanceSecurityQuestionService.addQuestion(question);
            } catch (DataAccessException e) {
                result.setCode(CommonCode.FAIL);
                result.setStatus(Status.FAIL);
                result.setDescription("添加失败：数据库问题");
                logger.error("添加失败 原因 ：--->", e.getMessage());
            } catch (Exception e) {
                result.setCode(CommonCode.FAIL);
                result.setStatus(Status.FAIL);
                result.setDescription("添加问题失败 ：未知错误");
                logger.error("添加失败 原因 ：--->", e.getMessage());
            }
        }
        return result;
    }

    @Override
    public Result updateQuestion(String questionId, String question) {
        logger.info("更新问题入参 question->{}", question);
        Result result = new Result();
        if (!StringUtil.isNotNull(question)) {
            result.setCode(CommonCode.FAIL);
            result.setStatus(Status.FAIL);
            result.setDescription("更新问题失败、问题不能为空");
        } else {
            try {
                advanceSecurityQuestionService.addQuestion(question);
            } catch (DataAccessException e) {
                result.setCode(CommonCode.FAIL);
                result.setStatus(Status.FAIL);
                result.setDescription("更新失败：数据库问题");
                logger.error("更新失败 原因 ：--->", e.getMessage());
            } catch (Exception e) {
                result.setCode(CommonCode.FAIL);
                result.setStatus(Status.FAIL);
                result.setDescription("更新问题失败 ：未知错误");
                logger.error("更新失败 原因 ：--->", e.getMessage());
            }
        }
        return null;
    }
}
